package serializer

import (
	"singo/model"
)

// Organization 序列化器
type Organization struct {
	// ID        uint   `json:"id"`
	UID       string `json:"id"`
	Name      string `json:"name"`
	Intro     string `json:"intro"`
	Avatar    string `json:"avatar"`
	Score     uint   `json:"score"`
	ParentID  string `json:"parent_id"`
	Head      string `json:"head"`
	CreatedBy string `json:"created_by"`
}

// OrganizationSimple 简单版序列化器
type OrganizationSimple struct {
	UID       string `json:"id"`
	Name      string `json:"name"`
	Head      string `json:"head"`
	CreatedBy string `json:"created_by"`
}

// BuildOrganizationSimple 简单版序列化
func BuildOrganizationSimple(organizationID string) OrganizationSimple {
	organization := model.Organization{}
	model.DB.First(&organization, "uid = ?", organizationID)

	return OrganizationSimple{
		UID:  organization.UID,
		Name: organization.Name,
		Head: organization.Head,
	}
}

// BuildOrganization 序列化
func BuildOrganization(item model.Organization) Organization {
	return Organization{
		UID:       item.UID,
		Name:      item.Name,
		Intro:     item.Intro,
		Avatar:    item.Avatar,
		Score:     item.Score,
		ParentID:  item.ParentID,
		Head:      item.Head,
		CreatedBy: item.CreatedBy,
	}
}

// BuildOrganizations 序列化列表
func BuildOrganizations(items []model.Organization) (organizations []Organization) {
	for _, item := range items {
		organization := BuildOrganization(item)
		organizations = append(organizations, organization)
	}
	return organizations
}
